package Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/cookieservlet")
public class cookIndex extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.print("<h1>Hello cookie</h1>");
        String username="admin";
        String password="123123";
        Cookie c1=new Cookie("name",username);
        Cookie c2=new Cookie("pwd",password);
        c1.setMaxAge(60);
        c2.setMaxAge(60);
        resp.addCookie(c1);
        resp.addCookie(c2);
        System.out.println("第一次添加Cookie");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
